package com.agileboot.api.controller.app;

import cn.hutool.core.io.file.FileNameUtil;
import com.agileboot.api.customize.config.ApiSecurityConfig;
import com.agileboot.common.constant.Constants;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.utils.ServletHolderUtil;
import com.agileboot.common.utils.file.FileUploadUtils;
import com.agileboot.domain.app.user.command.AppLoginDTO;
import com.agileboot.domain.app.user.db.AppUserInfoService;
import com.agileboot.domain.app.user.db.AppUserMapper;
import com.agileboot.domain.app.user.db.AppUserService;
import com.agileboot.domain.common.dto.UploadDTO;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import com.agileboot.infrastructure.user.app.AppLoginUser;
import com.agileboot.domain.app.user.db.AppUserInfoEntity;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

@RestController
@RequestMapping("/app")
@AllArgsConstructor
public class UserController {

    private final AppUserInfoService userInfoService;

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/user/info")
    public ResponseDTO<?> getUserInfo() {
        // 基础用户信息
        AppLoginUser appLoginUser = AuthenticationUtils.getAppLoginUser();
        appLoginUser.setLoginInfo(null);
        // 查找用户扩展信息
        AppUserInfoEntity userInfo = userInfoService.findUserInfo(appLoginUser.getUserId());

        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("userInfo", appLoginUser);
        hashMap.put("extend", userInfo);

        return ResponseDTO.ok(hashMap);
    }


    private final ApiSecurityConfig securityConfig;

    /**
     * 重置密码
     *
     * @return
     */
    @PostMapping("/user/restPassword")
    public ResponseDTO<?> restPassword(AppLoginDTO command) {
        AppLoginUser appLoginUser = AuthenticationUtils.getAppLoginUser();
        appLoginUser.setPassword(securityConfig.encoderPassword(command.getPassword()));

        // todo qqz 查找用户信息，更新密码
        return ResponseDTO.ok();
    }


    /**
     * 文件上传(多个、单个)
     *
     * @param files 文件
     * @return 文件信息
     */
    @PostMapping("uploads/files")
    public ResponseDTO<?> uploadFiles(@RequestParam("files") MultipartFile[] files) {

        ArrayList<UploadDTO> dtoArrayList = new ArrayList<>();
        Arrays.stream(files).filter(file -> !file.isEmpty()).forEach(file -> {
            // 保存到硬盘上
            String filename = FileUploadUtils.upload(Constants.UploadSubDir.UPLOAD_PATH, file);
            // 返回给前端
            String fileUrl = ServletHolderUtil.getContextUrl() + ":8090" + filename;
            // 组装返回数据
            UploadDTO uploadDTO = UploadDTO.builder().url(fileUrl).fileName(filename).newFileName(FileNameUtil.getName(filename)).originalFilename(file.getOriginalFilename()).build();
            dtoArrayList.add(uploadDTO);
        });

        return ResponseDTO.ok(dtoArrayList);
    }

}
